<template>
  <div class="box">
    <van-nav-bar
      class="nav"
      title="登录"
      left-arrow
      @click-left="onClickLeft"
    />
    <h2>优点达资讯</h2>
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        left-icon="manager"
        name="mobile"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        left-icon="lock"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button class="button" round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
      <van-nav-bar
        right-text="没有账号去注册 >"
        @click-right="$router.push('/register')"
      />
    </van-form>
  </div>
</template>

<script>
import { loginAPI } from '@/api/login'
import { Toast } from 'vant'

export default {
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    onClickLeft () {
      this.$router.go(-1)
    },
    async onSubmit (val) {
      const { data: res } = await loginAPI(val)
      console.log(res)
      if (res.errmsg === '登录成功') {
        // 存储账号信息
        localStorage.setItem('ydd-moblie-username', this.username)

        // 提示信息
        Toast.success('登录成功')
        // 存储token
        localStorage.setItem('ydd-mobile', res.data.token)
        // 页面跳转到登录
        this.$router.push('/user')
      } else {
        Toast.fail(res.errmsg)
      }
    }
  }
}
</script>

<style scoped lang="less">
.box {
  // 导航
  .nav {
    background-color: #003366;
    ::v-deep .van-nav-bar__title {
      color: #fff;
    }
  }
  // 底部去注册按钮
  .van-nav-bar {
    ::v-deep .van-nav-bar__text {
      color: #003366;
    }
  }

  // 返回按钮
  ::v-deep .van-nav-bar .van-icon {
    color: #fff;
  }
  .button {
    background-color: #003366;
    border: none;
  }

  h2 {
    font-weight: 400;
    text-align: center;
    color: #003366;
  }
  ::v-deep .van-hairline--bottom::after {
    border-bottom: 0;
  }
}
</style>
